using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for ProcedureControl.
	/// </summary>
	public class ProcedureControl : AdminUserControl
	{
    private FreeFlowAdministrator.AttachmentsControl attachmentsControl;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.TextBox lastPublishedLabel;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.PictureBox pictureBox1;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ProcedureControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the details for the procedure", new ActionExecuteDelegate(RefreshProcedure));
      actionList.Add("Delete", "Delete this procedure", new ActionExecuteDelegate(DeleteProcedure));
      actionList.Add("Save latest...", "Save the latest version of this procedure", 
        new ActionExecuteDelegate(SaveLatest));
      actionList.Add("Attachments");
      actionList.Add(attachmentsControl.ActionList);
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ProcedureControl));
      this.attachmentsControl = new FreeFlowAdministrator.AttachmentsControl();
      this.panel1 = new System.Windows.Forms.Panel();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.lastPublishedLabel = new System.Windows.Forms.TextBox();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.label3 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.label1 = new System.Windows.Forms.Label();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.panel1.SuspendLayout();
      this.SuspendLayout();
      // 
      // attachmentsControl
      // 
      this.attachmentsControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.attachmentsControl.Location = new System.Drawing.Point(0, 64);
      this.attachmentsControl.Name = "attachmentsControl";
      this.attachmentsControl.Size = new System.Drawing.Size(658, 312);
      this.attachmentsControl.TabIndex = 13;
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.pictureBox1);
      this.panel1.Controls.Add(this.lastPublishedLabel);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Controls.Add(this.label3);
      this.panel1.Controls.Add(this.label2);
      this.panel1.Controls.Add(this.label1);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(658, 64);
      this.panel1.TabIndex = 14;
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(616, 8);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.TabIndex = 5;
      this.pictureBox1.TabStop = false;
      // 
      // lastPublishedLabel
      // 
      this.lastPublishedLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
      this.lastPublishedLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.lastPublishedLabel.Location = new System.Drawing.Point(88, 24);
      this.lastPublishedLabel.Name = "lastPublishedLabel";
      this.lastPublishedLabel.ReadOnly = true;
      this.lastPublishedLabel.Size = new System.Drawing.Size(520, 13);
      this.lastPublishedLabel.TabIndex = 4;
      this.lastPublishedLabel.Text = "lastPublishedLabel";
      // 
      // nameLabel
      // 
      this.nameLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
      this.nameLabel.BorderStyle = System.Windows.Forms.BorderStyle.None;
      this.nameLabel.Location = new System.Drawing.Point(88, 8);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.ReadOnly = true;
      this.nameLabel.Size = new System.Drawing.Size(520, 13);
      this.nameLabel.TabIndex = 3;
      this.nameLabel.Text = "nameLabel";
      // 
      // label3
      // 
      this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label3.Location = new System.Drawing.Point(8, 48);
      this.label3.Name = "label3";
      this.label3.TabIndex = 2;
      this.label3.Text = "Attachments";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 24);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(80, 16);
      this.label2.TabIndex = 1;
      this.label2.Text = "Last published:";
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 8);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(100, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Name:";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(658, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 376);
      this.uiActionsControl.TabIndex = 15;
      // 
      // ProcedureControl
      // 
      this.Controls.Add(this.attachmentsControl);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "ProcedureControl";
      this.Size = new System.Drawing.Size(808, 376);
      this.panel1.ResumeLayout(false);
      this.ResumeLayout(false);

    }
		#endregion

    private Procedure procedure;
    public void LoadProcedure(Procedure procedure)
    {
      this.procedure = procedure;
      nameLabel.Text = procedure.Name;
      lastPublishedLabel.Text = procedure.LastPublished.ToString();
      attachmentsControl.LoadAttachments(procedure.Attachments);
    }

    private string SaveLatest()
    {
      VersionControl.SaveVersion(procedure.Versions.LatestVersion);
      return null;
    }

    private string DeleteProcedure()
    {
      if (MessageBox.Show("Delete this procedure?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          procedure.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return null;
    }

    private string RefreshProcedure()
    {
      procedure.Refresh();
      LoadProcedure(procedure);
      OnRefreshRequired();
      return null;
    }
	}
}
